package com.atguigu.school.service.impl;

import com.atguigu.school.commons.Page;
import com.atguigu.school.mapper.ClazzMapper;
import com.atguigu.school.pojo.Clazz;
import com.atguigu.school.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    /**
     * 访问班级数据
     */
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public void saveOrUpdateClazz(Clazz clazz) {
        if (clazz.getId() == null || clazz.getId() == 0) {
            this.clazzMapper.insertClazz(clazz);
        } else {
            this.clazzMapper.updateClazzById(clazz);
        }
    }

    @Override
    public void deleteClazzByIds(List<Integer> ids) {
        this.clazzMapper.deleteClazzByIds(ids);
    }

    @Override
    public Page<Clazz> selectClazzByNameAndGradeName(String name, String gradeName, Integer current, Integer size) {
        Integer total = this.clazzMapper.countClazzByNameAndGradeName(name, gradeName);
        Page<Clazz> page = new Page<>(current, size, total);
        List<Clazz> clazzList = this.clazzMapper.selectClazzByNameAndGradeName(name, gradeName, page.getOffset(), size);
        page.setRecords(clazzList);
        return page;
    }

    @Override
    public List<Clazz> selectClazz() {
        return this.clazzMapper.selectClazz();
    }

}
